package org.jeecg.cn.edu.scau.vm.container.service.impl;

import com.alibaba.fastjson.JSON;
import org.jeecg.cn.edu.scau.vm.container.domain.VmContainer;
import org.jeecg.cn.edu.scau.vm.container.enums.ContainerStatusEnum;
import org.jeecg.cn.edu.scau.vm.container.request.CreateContainerRequest;
import org.jeecg.cn.edu.scau.vm.container.request.RemoveContainerRequest;
import org.jeecg.cn.edu.scau.vm.container.request.StartContainerRequest;
import org.jeecg.cn.edu.scau.vm.container.request.StopContainerRequest;
import org.jeecg.cn.edu.scau.vm.container.service.VmContainerService;
import org.jeecg.common.api.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static org.jeecg.cn.edu.scau.vm.common.consts.MQConst.ExchangeName.*;

@Service
public class VmContainerServiceImpl implements VmContainerService {
    @Resource
    private RabbitTemplate rabbitTemplate;
    private static final Logger logger = LoggerFactory.getLogger(VmContainerServiceImpl.class);

    @Override
    public Result<String> containerCreateEventPublish(VmContainer vmContainer, String command, String timeout) {
        // 1. 构造消息
        CreateContainerRequest request = CreateContainerRequest.builder()
                .command(command.replaceAll("\\r?\\n$", ""))
                .timeout(timeout)
                .tableId(vmContainer.getId())
                .build();
        Message message = new Message(JSON.toJSONString(request).getBytes());
        rabbitTemplate.send(CONTAINER_CREATE_EVENT_EXCHANGE, vmContainer.getNodeId(), message);
        logger.info("容器创建任务已经下发,消息内容:{}=>{},nodeId:{}", request, CONTAINER_CREATE_EVENT_EXCHANGE, vmContainer.getNodeId());
        // 2. 容器信息在数据库暂存(基础配置)
        vmContainer.setStatus(ContainerStatusEnum.CREATE_INIT.getCode());
        vmContainer.setContainerId("");
        return Result.OK("容器创建任务已经下发,请稍后到我的容器中进行查看!");
    }

    @Override
    public Result<String> containerStopEventPublish(String nodeId, String containerId, String tableId) {
        // 1. 构造请求对象
        StopContainerRequest request = StopContainerRequest.builder()
                .containerId(containerId)
                .tableId(tableId)
                .build();
        // 2. 发布消息
        Message message = new Message(JSON.toJSONString(request).getBytes());
        rabbitTemplate.send(CONTAINER_STOP_EVENT_EXCHANGE, nodeId, message);
        logger.info("容器停止任务已经下发,消息内容:{}=>{},nodeId:{}", request, CONTAINER_STOP_EVENT_EXCHANGE, nodeId);
        return Result.ok("容器正在停止中,请稍后到我的容器中进行查看!");
    }

    @Override
    public Result<String> containerRemoveEventPublish(String nodeId, String containerId, String tableId) {
        // 1. 构造删除消息对象
        RemoveContainerRequest request = RemoveContainerRequest.builder()
                .containerId(containerId)
                .tableId(tableId)
                .timeout(1000)
                .build();
        Message message = new Message(JSON.toJSONString(request).getBytes());
        rabbitTemplate.send(CONTAINER_REMOVE_EVENT_EXCHANGE, nodeId, message);
        logger.info("容器删除任务已经下发,消息内容:{}=>{},nodeId:{}", request, CONTAINER_REMOVE_EVENT_EXCHANGE, nodeId);
        return Result.ok("容器正在删除中,请稍后到我的容器中进行查看!");
    }

    @Override
    public Result<String> containerStartEventPublish(String nodeId, String containerId, String tableId) {
        // 1. 构造请求对象
        StartContainerRequest request = StartContainerRequest.builder()
                .containerId(containerId)
                .build();
        // 2. 发布消息
        Message message = new Message(JSON.toJSONString(request).getBytes());
        rabbitTemplate.send(CONTAINER_STOP_EVENT_EXCHANGE, nodeId, message);
        logger.info("容器启动任务已经下发,消息内容:{}=>{},nodeId:{}", request, CONTAINER_STOP_EVENT_EXCHANGE, nodeId);
        return Result.ok("容器正在启动中,请稍后到我的容器中进行查看!");
    }


}
